package com.jinlejia.erp.ui.mine.sign;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.DetailActivityFieldSignInBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.DateUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 外勤打卡
 */
public class DetailFieldSignInActivity extends AppCompatActivity {
    private String imgUrl, reason, address;
    private double lat, lng;
    private DetailActivityFieldSignInBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.detail_activity_field_sign_in);
        binding.setEvent(this);
        getData();
    }

    private void getData() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", getIntent().getStringExtra("id"));
        if (getIntent().hasExtra("pin"))
            jsonObject.put("pin", getIntent().getStringExtra("pin"));
        OkHttpUtils
                .postString()
                .url(ApiConfig.DETAIL_FIELD_OUT)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        call.cancel();
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(DetailFieldSignInActivity.this, response)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            //签到时间
                            binding.time.setText(DateUtil.getTime(data.getLongValue("signTime"), "HH:mm"));
                            //外出事项
                            binding.outItemsReason.setText(data.getString("outType"));
                            reason = data.getString("outReason");
                            if (TextUtils.isEmpty(reason)) {
                                binding.outItemsReason.setVisibility(View.GONE);
                            }
                            //签到地点
                            address = data.getString("position");
                            binding.currentLocation.setText(address);
                            lat = data.getDoubleValue("latitude");
                            lng = data.getDoubleValue("longitude");
                            //图片
                            if (data.containsKey("sceneImgUrl"))
                                imgUrl = ApiConfig.MAGEHEADER_URL().concat(data.getString("sceneImgUrl"));
                            Glide.with(DetailFieldSignInActivity.this)
                                    .load(imgUrl)
                                    .placeholder(R.mipmap.ic_no_img)
                                    .error(R.mipmap.ic_no_img)
                                    .into(binding.siteImg);

                        }
                    }
                });
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.out_items_reason:
                DialogOutReason dialogOutReason = new DialogOutReason(DetailFieldSignInActivity.this, reason);
                dialogOutReason.show();
                break;
            case R.id.current_location:
                Intent locIntent = new Intent(DetailFieldSignInActivity.this, SignLocationActivity.class);
                locIntent.putExtra("lat", lat);
                locIntent.putExtra("lng", lng);
                locIntent.putExtra("address", address);
                startActivity(locIntent);
                break;
            case R.id.site_img:
                Intent imgIntent = new Intent(DetailFieldSignInActivity.this, ZoomImageActivity.class);
                imgIntent.putExtra("img_url", imgUrl);
                startActivity(imgIntent);
                break;
            default:
                break;
        }
    }
}
